/*
 * LeadListFiltersPresenter.java
 * 
 * Copyright 2011 Matt Whipple <matt@mattwhipple.com>
 * 
 * This file is part of com.teamsuperfitness.apps
 * 
 * com.teamsuperfitness.apps is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * com.teamsuperfitness.apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with com.teamsuperfitness.apps. If not, see http://www.gnu.org/licenses/.
 */

package com.teamsuperfitness.apps.client.presenters;

import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.teamsuperfitness.apps.shared.events.FilterUpdateEvent;
import com.teamsuperfitness.apps.shared.events.UserChangedEvent;
import com.teamsuperfitness.apps.shared.events.UserChangedEventHandler;
import com.teamsuperfitness.apps.shared.model.Filter;

/**
 * Allows user to apply filters on lists, also handles user based filtering
 * @author mwhipple
 *
 */
public class LeadListFiltersPresenter implements IPresenter {
	final Display display;
	final SimpleEventBus eventBus;
	
	public interface Display {
		Widget asWidget();
	}
	
	public LeadListFiltersPresenter(SimpleEventBus eventBus, Display view) {
		this.eventBus = eventBus;
		this.display = view;
		bind();
	}
	
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}
	
	public void bind() {
		eventBus.addHandler(UserChangedEvent.TYPE, new UserChangedEventHandler() {
			public void onUserChanged(UserChangedEvent event) {
				Filter filter = new Filter();
				filter.id = "User";
				filter.property = "rep";
				filter.operator = "=";
				filter.value = event.getAppUser().getNickname();
				eventBus.fireEvent(new FilterUpdateEvent(filter));
			}});
	}

}
